package exercise4;

public class Sort {

    public void quickSort(int[] array) {
        quick(array, 0, array.length - 1);
    }

    public void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length - 1;
        while (end > 0) {
            swap(array,0,end);
            end--;
            siftDwn(array,0,end);
        }
    }

    public void shallSort (int[] array) {
        int gap = array.length / 2;
        while (gap > 0) {
            shall(array ,gap);
            gap /= 2;
        }
    }

    public void mergeSort (int[] array) {
        mergeFun(array,0,array.length-1);
    }

    private void mergeFun(int[] array,int start, int end) {
        if (start >= end) {
            return;
        }
        int midIndex = (end - start) / 2 + start;

        mergeFun(array,start,midIndex);
        mergeFun(array,midIndex + 1, end);

        merge(array,midIndex,start,end);
    }

    private void merge (int[] array, int midIndex, int start, int end) {
        int[] arr = new int[end - start + 1];
        int se = midIndex + 1;
        int k = start;
        int i = 0;
        while (start <= midIndex && se <= end) {
            if (array[start] < array[se]) {
                arr[i++] = array[start++];
            } else {
                arr[i++] = array[se++];
            }
        }
        while (start <= midIndex) {
            arr[i++] = array[start++];
        }
        while (se <= end) {
            arr[i++] = array[se++];
        }
        for (int j = k; j <= end; j++) {
            array[j] = arr[j - k];
        }
    }

    private void shall (int[] array , int gap) {
        for (int i = gap; i < array.length; i++ ) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j-=gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    private void createBigHeap(int[] array) {
        for (int parent = (array.length-1-1)/2 ; parent >= 0; parent--) {
            siftDwn(array,parent,array.length-1);
        }
    }

    private void siftDwn(int[] array, int parent, int end) {
        int child = parent*2 + 1;
        while (child <= end) {
            if (child + 1 <= end && array[child] < array[child+1]) {
                child++;
            }
            if (array[child] > array[parent]) {
                swap(array,child,parent);
                parent = child;
                child = 2 * child + 1;
            } else {
                break;
            }
        }
    }

    private void quick(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        if (end - start + 1 <= 15) {
            insertWithQuick(array,start,end);
            return;
        }
        int part = partition2(array, start, end);

        quick(array, start, part - 1);
        quick(array, part + 1, end);
    }

    private void insertWithQuick(int[] array, int left, int right) {
        for (int i = left + 1; i <= right; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j+1] = array[j];
                } else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    private int partition2(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    private int partition(int[] array, int left, int right) {
        int tmp = array[left];
        int i = left;
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array, left, right);
        }
        swap(array, i, left);
        return left;
    }

    private void swap(int[] array, int x, int y) {
        int tmp = array[x];
        array[x] = array[y];
        array[y] = tmp;
    }

}
